Ext.Loader.setConfig({enabled: true});
Ext.Loader.setPath({'Ext.ux' : 'extjs4/ux'});
Ext.require([
             'Ext.grid.*',
             'Ext.data.*',
             'Ext.ux.grid.FiltersFeature',
             'Ext.toolbar.Paging',
             'Ext.ux.ajax.JsonSimlet',
             'Ext.ux.ajax.SimManager',
             'Ext.ux.grid.Printer'           
]);
Ext.define('TS.view.sale.SaleBillList',{
	extend: 'Ext.panel.Panel',
	border: false,
	bodyPadding:0,
	layout: 'border',
	alias: 'widget.salebilllist',
	title: '销售单列表',
	items: [
		    {
		    	xtype: 'panel',
		    	title: '检索',
		    	region: 'north',
		    	height: 100,
		    	collapsible: true,
		    	border: false,
		    	buttonAlign: 'left',
		    	header: {iconCls: 'bill-search-button'},
		    	layout: 'hbox',
		    	items: [
		    	    {xtype: 'hiddenfield',name:'custId'},
		    	    {xtype: 'datefield',name:'begDate',fieldLabel: '开始日期:',labelWidth:60,width:160,format:'Y-m-d',value:new Date(),margin:'10 10 10 10'},
		    	    {xtype: 'datefield',name:'endDate',fieldLabel: '截止日期:',labelWidth:60,width:160,format:'Y-m-d',value:new Date(),margin:'10 10 10 10'},
		    	    {
		    	    	xtype: 'triggerfield',name:'customer',fieldLabel:'客户:',labelWidth:30,width:310,margin:'10 10 10 10',
		    	    	emptyText:'请输入客户编码或名称',trigger1Cls:Ext.baseCSSPrefix + 'form-search-trigger',
		    	    	onTriggerClick:function(e){e.keyCode=e.ENTER;this.fireEvent('specialkey',this,e);}
		    	    },		    	    
		    	    {xtype: 'button',text:'查询',margin:'10 10 10 10',action:'search',iconCls:'bill-find-button'},
		    	    {xtype: 'button',text:'高级检索',margin:'10 10 10 10',action:'advsearch',iconCls:'bill-search-button'}
		    	]
		    },
		    {
		    	xtype:'tabpanel',
		    	region:'center',
		    	items:[
				{
					xtype:'gridpanel',
					title:'查询结果',
					header:false,
					border:false,
					//region:'center',
					store:'sale.SaleBillStore',
					selType:'checkboxmodel',//设定选择模式
					multiSelect:true,//运行多选
				    features: [{
				        ftype: 'summary'
				    },{
				        ftype: 'filters',
				        // encode and local configuration options defined previously for easier reuse
				        encode: false, // json encode the filter query
				        local: true,   // defaults to false (remote filtering)
	
				        filters: [{
				            type: 'string',
				            dataIndex: 'billCode'
				        },{
				        	type:'date',
				        	dataIndex:'billDate'
				        },{
				        	type:'list',
				        	options:['销售出库单','其它出库单'],
				        	dataIndex:'billName'
				        },{
				        	type:'string',
				        	dataIndex:'customer.customerCode'
				        },{
				        	type:'boolean',
				        	dataIndex:'archived'
				        },{
				        	type:'numeric',
				        	dataIndex:'fyear'
				        },{
				        	type:'numeric',
				        	dataIndex:'period'
				        },{
				        	type:'string',
				        	dataIndex:'remark'
				        }]			    	
				    }],					    
					columns:[
							   {header:'年份',dataIndex:'fyear',width:50,align:'right'},
							   {header:'期间',dataIndex:'period',width:50,align:'right'},
					       {
					    	   header:'单据编号',dataIndex:'billCode',width:100,
					    	   summaryType:'count',summaryRenderer: function(value, summaryData, dataIndex) {return '<b>' + Ext.String.format('合计[{0}]:', value) + '</b>'; }
					       },
						   {header:'单据类型',dataIndex:'billName',width:80},
						   {header:'日期',dataIndex:'billDate',width:80,renderer : Ext.util.Format.dateRenderer('Y-m-d')},
						   {header:'客户编码',dataIndex:'Customer',filterable:true,width:80,renderer:function(value,metaData,record){return record.getcustomer().get('customerCode');}},
						   {header:'客户名称',dataIndex:'customer',width:250,renderer:function(value,metaData,record){return record.getcustomer().get('customerName');}},
						   {header:'单据金额',dataIndex:'Amount',width:100,align:'right',
							   renderer:function(value,metaData,record){
							   		return record.getbillAmount().get('billAmount');
							   },
							   summaryType:function (records) {
								    var result = [], retval;
								    Ext.Array.each(records, function (record){
								        result.push(record.getbillAmount().get('billAmount'));
								    });       
								    retval = Ext.Array.sum(result); 
								    return Ext.util.Format.number(retval, '0.00');
							   },
							   summaryRenderer:function(value,summaryData,dataIndex){
							   		return '<b>' + Ext.util.Format.number(value,'0,000.00') + '</b>';
							   }
						   },
						   {header:'成本金额',dataIndex:'CostAmount',width:100,align:'right',
							   renderer:function(value,metaData,record){
							   		return record.getbillAmount().get('billCostAmount');
							   },
							   summaryType:function (records) {
								    var result = [], retval;
								    Ext.Array.each(records, function (record){
								        result.push(record.getbillAmount().get('billCostAmount'));
								    });       
								    retval = Ext.Array.sum(result); 
								    return Ext.util.Format.number(retval, '0.00');
							   },
							   summaryRenderer:function(value,summaryData,dataIndex){
							   		return '<b>' + Ext.util.Format.number(value,'0,000.00') + '</b>';
						   }},
						   {header:'毛利',dataIndex:'Profit',width:80,align:'right',
						   		renderer:function(value,metaData,record){
						   			return record.getbillAmount().get('billProfit');
						   		},
						   		summaryType:function (records) {
								    var result = [], retval;
								    Ext.Array.each(records, function (record){
								        result.push(record.getbillAmount().get('billProfit'));
								    });       
								    retval = Ext.Array.sum(result); 
								    return Ext.util.Format.number(retval, '0.00');
							    },
						   		summaryRenderer:function(value,summaryData,dataIndex){
						   			return '<b>' + Ext.util.Format.number(value,'0,000.00') + '</b>';
						   }},
						   {header:'结算金额',dataIndex:'SettleAmount',width:100,align:'right',
							   renderer:function(value,metaData,record){
							   		return record.getbillAmount().get('billSettleAmount');
							   },
							   summaryType:function (records) {
								    var result = [], retval;
								    Ext.Array.each(records, function (record){
								        result.push(record.getbillAmount().get('billSettleAmount'));
								    });       
								    retval = Ext.Array.sum(result); 
								    return Ext.util.Format.number(retval, '0.00');
							   },
							   summaryRenderer:function(value,summaryData,dataIndex){
							   		return '<b>' + Ext.util.Format.number(value,'0,000.00') + '</b>';
						   }},
						   {header:'归档状态',dataIndex:'archived',width:60,renderer:function(value){if(value) return '是'; else return '否';}},
						   {header:'部门',dataIndex:'dept',width:65,renderer:function(value,metaData,record){return record.getdept().get('deptName');}},
						   {header:'业务员',dataIndex:'emp',width:65,renderer:function(value,metaData,record){return record.getemp().get('empName');}},
						   {header:'制单人',dataIndex:'maker',width:65,renderer:function(value,metaData,record){return record.getmaker().get('empName');}},
						   {header:'备注',dataIndex:'remark',width:200}
					],
					dockedItems:[{
						xtype:'pagingtoolbar',
						store:'sale.SaleBillStore',
						dock:'bottom',
						displayInfo:true,
						items:[{
							text:'清除过滤条件',
							iconCls:'button-clear-filter',
							handler:function(){
								var grid = this.up('gridpanel');
								grid.filters.clearFilters();
							}
						}]
					}],
					tbar:[
							{xtype:'button',text:'新增',action:'createBill',iconCls:'bill-add-button'},
							{xtype:'button',text:'编辑',action:'editBill',iconCls:'bill-edit-button'},
							{xtype:'button',text:'删除',action:'deleteBill',iconCls:'bill-delete-button'},
							{xtype:'button',text:'归档',action:'archiveBill',iconCls:'bill-archive-button'},
							{xtype:'button',text:'反归档',action:'unArchiveBill',iconCls:'bill-unarchive-button'},
							{xtype:'button',text:'打印',action:'printBill',iconCls:'bill-printer-button'},
							{xtype:'button',text:'导出',action:'exportBill',iconCls:'bill-export-button'}					
					]
				},
				{
	    		    	xtype:'grid',
	    				title:'单据明细',
	    				header:false,
	    				//border:1,
	    				//alias : 'widget.salebilldetails',
	    				name:'salebilldetails',
	    				store:'sale.SaleBillMasterDetailStore',
	    				region:'center',
	    				margin : '0 0 0 0',
	    				anchor:'100% 55%',
	    			    features: [{ftype: 'summary'
				    		},{
				            ftype: 'groupingsummary',
				            groupHeaderTpl: '{name}',
				            hideGroupedHeader: true,
				            enableGroupingMenu: false
				        }],
	    				columns:[
					         {header:'单据编号',dataIndex:'billCode',width:100,summaryType:'count',summaryRenderer: function(value, summaryData, dataIndex) {return '<b>' + Ext.String.format('合计[{0}]:', value) + '</b>';}},
						     {header:'单据类型',dataIndex:'billName',width:80},
						     {header:'日期',dataIndex:'billDate',width:80,renderer : Ext.util.Format.dateRenderer('Y-m-d')},
						     {header:'客户编码',dataIndex:'customerCode'},
						     {header:'客户名称',dataIndex:'customerName',width:120},
							 {header:'商品编号',dataIndex:'productCode',width:60},
					         {header:'商品名称',dataIndex:'productName',width:120,summaryType:'count',summaryRenderer: function(value, summaryData, dataIndex) {return '<b>' + Ext.String.format('合计:[{0}]', value) + '</b>';}},
					         {header:'规格',dataIndex:'packSpec',width:80},
					         {header:'生产企业',dataIndex:'manuf',width:100},
					         {header:'单位',dataIndex:'unit',width:50},
					         {header:'数量',dataIndex:'quantity',align:'right',summaryType:'sum',summaryRenderer:function(value,summaryData,dataIndex){return '<b>' + Ext.String.format('数量:[{0}]', Ext.util.Format.number(value,'0,000.000')) + '</b>';},renderer : Ext.util.Format.numberRenderer('0.000')},
					         {header:'单价',dataIndex:'price',align:'right'},
					         {header:'成本价',dataIndex:'costPrice',align:'right'},
					         {header:'税率',dataIndex:'taxRate',align:'right'},
					         {header:'金额',dataIndex:'amount',align:'right',summaryType:'sum',summaryRenderer:function(value,summaryData,dataIndex){return '<b>' + Ext.String.format('金额:[{0}]', Ext.util.Format.number(value,'0,000.000') )+ '</b>';},renderer : Ext.util.Format.numberRenderer('0.000')},
					         {header:'成本金额',dataIndex:'costAmount',align:'right',summaryType:'sum',summaryRenderer:function(value,summaryData,dataIndex){return '<b>' + Ext.String.format('成本金额:[{0}]', Ext.util.Format.number(value,'0,000.000') )+ '</b>';},renderer : Ext.util.Format.numberRenderer('0.000')},
					         {header:'批号',dataIndex:'batchNo',width:80},
					         {header:'生产日期',dataIndex:'manufDate',width:80,renderer : Ext.util.Format.dateRenderer('Y-m-d')},
					         {header:'有效期',dataIndex:'expDate',width:80,renderer : Ext.util.Format.dateRenderer('Y-m-d')},
					         {header:'结算价',dataIndex:'settlePrice',align:'right',renderer : Ext.util.Format.numberRenderer('0.000')},
					         {header:'归档状态',dataIndex:'archived',width:60,renderer:function(value){if(value) return '是'; else return '否';}},
						     {header:'部门',dataIndex:'deptName',width:65},
						     {header:'业务员',dataIndex:'empName',width:65},
						     {header:'制单人',dataIndex:'makerName',width:65},
						     {header:'备注',dataIndex:'remark',width:200},
					         {header:'年份',dataIndex:'fyear',width:50,align:'right'},
							 {header:'期间',dataIndex:'period',width:50,align:'right'}
	    				],
	    				tbar:[{
							xtype:'combobox',
							listConfig:{
								maxHeight:200
							},
							labelWidth:50,
							fieldLabel:'分组条件:',
							width:300,
							queryMode:'local',//[local|remot]
							store: Ext.create('Ext.data.Store',{fields:[{name:'showName'},{name:'groupName'}],data:[{groupName:'null',showName:'-----'},{groupName:'productName',showName:'按商品分组统计'},{groupName:'billCode',showName:'按单据分组统计'},{groupName:'customerName',showName:'按客户分组统计'},{groupName:'empName',showName:'按业务员分组统计'},{groupName:'deptName',showName:'按部门分组统计'}]}),
							valueField:"groupName",
							displayField :'showName',
							typeAhead : true,
							value:'null'
		                }]
				}]
		    }
	],	
	initComponent:function() {		
		
		this.callParent(arguments);
	}
});